// 2025/5/22
// 字符编码

#include <iostream>
#include <queue>
#include <unordered_map>
using namespace std;

int main() {
    string str;
    while(cin >> str)
    {
        unordered_map<char, int> mp;
        for(auto& ch : str)
        {
            mp[ch]++;
        }
        priority_queue<long long, vector<long long>, greater<long long>> heap;
        for(auto& pr : mp)
        {
            heap.push(pr.second);
        }
        long long ans = 0;
        while(heap.size() >= 2)
        {
            long long num1 = heap.top(); heap.pop();
            long long num2 = heap.top(); heap.pop();

            long long newnum = num1 + num2;
            ans += newnum;
            heap.push(newnum);
        }
        cout << ans << endl;
    }
}
// 64 位输出请用 printf("%lld")